/**
 * 生成随机数值的数组
 * @param {Number} total 
 * @param {Number} max 
 * @returns {Array<Namber>}
 */
function getDataArray(total, max) {
    let _data = [];
    for (let i = 0; i < total; i++) {
        _data.push(Math.floor(Math.random() * max) + 1);  //1~max的正整数数组
    }
    return _data;
}

/**
 * 输出数组
 * @param {Array<Namber>} data 
 */
function printArray(data) {
    for (let i = 0; i < data.length; i++) {
        if (i % 10 === 0) {
            document.write("<br>");
        }
        document.write(`&nbsp;${data[i]}&nbsp;`);
    }
    document.write("<br>");
}
/**
 * 冒泡排序
 * @param {Array<Namber>} data 
 * @returns {Array<Namber>}
 */
function bubbleSort(data) {
    let len = data.length;
    for (let i = 0; i < len - 1; i++) {
        for (let j = 0; j < len - 1 - i; j++) {
            if (data[j] > data[j + 1]) {
                let temp = data[j + 1];
                data[j + 1] = data[j];
                data[j] = temp;
            }
        }
    }
    return data;
}

/**
 * 选择排序
 * @param {Array<Namber>} data 
 * @returns {Array<Namber>}
 */
function selectionSort(data) {
    let len = data.length;
    let minIndex, temp;
    for (let i = 0; i < len - 1; i++) {
        minIndex = i;
        for (let j = i + 1; j < len; j++) {
            if (data[j] < data[minIndex]) {
                minIndex = j;
            }
        }
        temp = data[i];
        data[i] = data[minIndex];
        data[minIndex] = temp;
    }
    return data;
}

/**
 * 插入排序
 * @param {Array<Namber>} data 
 * @returns {Array<Namber>}
 */
function insertionSort(data) {
    let len = data.length;
    let preIndex, current;
    for (let i = 1; i < len; i++) {
        preIndex = i - 1;
        current = data[i];
        while (preIndex >= 0 && data[preIndex] > current) {
            data[preIndex + 1] = data[preIndex];
            preIndex--;
        }
        data[preIndex = 1] = current;
    }
    return data;
}

/**
 * 快速排序
 * @param {Array<Namber>} data 
 * @returns {Array<Namber>}
 */
function quickSort(data) {
    if (data.length <= 1)
        return data;
    let pivotIndex = Math.floor(data.length / 2);
    let pivot = data.splice(pivotIndex, 1)[0];
    let left = [];
    let right = [];
    for (let i = 0; i < data.length; i++) {
        if (data[i] < pivot) {
            left.push(data[i]);
        } else {
            right.push(data[i]);
        }
    }
    return quickSort(left).concat([pivot], quickSort(right));
}